package com.woniu.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.project.core.dto.ResultDto;
import com.woniu.project.core.exception.ProjectProductException;
import com.woniu.project.dto.product.ProductSelectDto;
import com.woniu.project.params.product.ProductAddParam;
import com.woniu.project.params.product.ProductSelectParam;
import com.woniu.project.params.product.ProductUpdateParam;
import com.woniu.project.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/product")
@Api(tags="类型管理")
public class ProductController {
    @Autowired
    IProductService productService;

    //根据不同条件进行不同的分页查询
    @ApiOperation(value="类型查询",notes = "参数均选填")
    @GetMapping("/select")
    public ResultDto select(@ModelAttribute ProductSelectParam productSelectParam) {

        try {
            IPage<ProductSelectDto> productIpage = productService.select(productSelectParam);
            return ResultDto.success(200, "查询成功", productIpage);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品类型查询系统异常");
        }

    }
    @ApiOperation(value="新增商品类型",notes = "id不填,其他参数均必填")
    //新增商品类型
    @PostMapping("/addProduct")
    public ResultDto addProduct(@Valid @RequestBody ProductAddParam productAddParam) {
        try {
            int result = productService.addProduct(productAddParam);
            if (result == 1) {
                return ResultDto.success(500, "该序号已存在，请重新输入", null);
            }
            return ResultDto.success(200, "新增商品类型成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品类型新增系统异常");
        }
    }
    @ApiOperation(value="修改商品类型",notes = "参数全必填")
    //修改商品类型
    @PutMapping("/updateProduct")
    public ResultDto updateProduct(@Valid @RequestBody ProductUpdateParam productUpdateParam) {
        try {
            int result = productService.updateProduct(productUpdateParam);
            if (result == 1) {
                return ResultDto.success(500, "该序号已存在，请重新输入", null);
            }
            return ResultDto.success(200, "修改商品类型成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品类型修改系统异常");
        }
    }
    @ApiOperation(value="根据id删除类型",notes = "id必填")
    //根据id删除类型
    @DeleteMapping("/deleteById/{id}")
    public ResultDto deleteById(@PathVariable Integer id) {
        try {
            productService.deleteById(id);
            return ResultDto.success(200, "删除商品类型成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品类型删除系统异常");
        }
    }
}

